package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ExChangeBean;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.ChatActivity;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/5/12.
 */

public class ChatP extends BaseP<ChatActivity>{
    public void exchange(String toChatUsername) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("easemob_id", toChatUsername);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        final RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().exChangeData(requestBody)
                .compose(XApi.<ExChangeBean>getApiTransformer())         //异常处理
                .compose(XApi.<ExChangeBean>getScheduler())              //线程切换
                .compose(getV().<ExChangeBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ExChangeBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ExChangeBean exChangeBean) {
                        if ("0".equals(exChangeBean.getError_code())) {
                            getV().exChangeSucceed(exChangeBean.getData().getActivity_id());
                        } else if ("1000".equals(exChangeBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().exChangeFail(exChangeBean.getMsg());
                        }
                    }
                });
    }
}
